﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using MPBlog.Core;
using MPBlog.DAL;
using MPBlog.Model;
using NHibernate.Criterion;

namespace MPBlog.UserControls
{
    /// <summary>
    /// 为ArticleDetail用户控件提供通用方法和属性
    /// </summary>
    public class ArticleDetailBase : UCBase
    {
        #region -controls define -

        protected Button btnAddComment;
        protected TextBox tbName;
        protected TextBox tbMail;
        protected TextBox tbComment;
        protected Repeater rptCommentList;

        #endregion

        #region - variable -

        private readonly ArticleDao articleDao = new ArticleDao();
        private readonly CommentDao commnetDao = new CommentDao();


        #endregion

        #region - property -

        /// <summary>
        /// 查看的文章实体
        /// </summary>
        public Article Article { get; set; }

        #endregion

        #region - event -

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            btnAddComment.OnClientClick =
                string.Format("if(!MPBlog.validate.addComment('{0}','{1}','{2}')){{return false;}}", tbName.ClientID,
                              tbMail.ClientID, tbComment.ClientID);

            int articleId;
            if (int.TryParse(Request.QueryString["articleId"], out articleId))
            {
                Article = articleDao.GetById(articleId);
                if (Article == null)
                {
                    //文章ID不存在
                    RedirectToArticleList();
                }
            }
            else
            {
                RedirectToArticleList();
            }

            rptCommentList.DataSource = commnetDao.GetList(Restrictions.Eq("Article",articleDao.GetById(articleId)));
            rptCommentList.DataBind();
        }

        protected void OnBtnAddCommentClick(object sender, EventArgs e)
        {
            #region -Get value and validate-

            string name = tbName.Text;
            string mail = tbMail.Text;
            string comment = tbComment.Text;
            int articleId;
            int replyToCommentId;

            if (string.IsNullOrEmpty(name))
            {
                Page.Message("不写您的大名就不对了呀", Utils.MessageType.error, 10000);
                return;
            }
            if (string.IsNullOrEmpty(mail))
            {
                Page.Message("不写邮箱其他人回复了就不好及时通知你拉", Utils.MessageType.error, 10000);
                return;
            }
            if (string.IsNullOrEmpty(comment))
            {
                Page.Message("请填写评论", Utils.MessageType.error, 10000);
                return;
            }

            if (!int.TryParse(Request.Form["articleId"], out articleId))
            {
                Page.Message("我不知道给哪篇文章评论呢，so,评论失败", Utils.MessageType.error, 10000);
                return;
            }
            if (!int.TryParse(Request.Form["replyToCommentId"], out replyToCommentId))
            {
                Page.Message("评论失败", Utils.MessageType.error, 10000);
                return;
            }

            #endregion

            Comment newComment = new Comment
                                  {
                                      CommnetAuthor = name,
                                      Email = mail,
                                      CommentContent = comment,
                                      Article = articleDao.GetById(articleId),
                                      TargetCommentId = replyToCommentId
                                  };

            Comment resComment = commnetDao.Create(newComment);
            if (resComment == null || resComment.Id <= 0)
            {
                Page.Message("评论失败", Utils.MessageType.error, 10000);
            }
            else
            {
                Page.Message("评论成功", Utils.MessageType.success, 5000);
            }
        }
        #endregion

        #region - method -

        private void RedirectToArticleList()
        {
            if (IsAdminRequest)
            {
                Response.Redirect("~/Admin/ArticleManage/ArticleList.aspx");
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }



        #endregion
    }
}